package com.jinlejia.bbs.logic;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.mine.AttentionBean;
import com.jinlejia.bbs.http.entity.AttentionEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/3/26.
 */
//我的关注
public class FansLogic {
    private final Context context;
    private final ArrayList<AttentionBean> dataList;
    private final LogicCallBack callBack;
    public FansLogic(Activity activity, ArrayList<AttentionBean> dataList, LogicCallBack callBack){
        this.context = activity;
        this.dataList = dataList;
        this.callBack = callBack;
    }

    private int pageIndex;

    public void refresh(String pin){
        pageIndex = 1;
        loadData(pageIndex,pin);
    }

    public void load(String pin){
        pageIndex = pageIndex+1;
        loadData(pageIndex,pin);
    }

    public void refresh(){
        pageIndex = 1;
        loadData(pageIndex,null);
    }

    public void load(){
        pageIndex = pageIndex+1;
        loadData(pageIndex,null);
    }

    private void loadData(final int pageIndex,String pin){
        JSONObject json = new JSONObject(pageIndex);
        if (pin!=null){
            json.put("pin",pin);
        }
        httpCallForm(BBSConfig.listAttentionedUser,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false);
                    }
                    @Override
                    public void onError(Exception e) {
                        if (callBack==null)return;
                        callBack.onResult(false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        if (callBack==null)return;
                        TypeReference<PageEntity<AttentionEntity>> reference = new TypeReference<PageEntity<AttentionEntity>>() {
                        };
                        PageEntity<AttentionEntity> response = parse.parsePageEntity(reference);
                        List<AttentionEntity> data = response.getData();
                        if (pageIndex==1){
                            dataList.clear();
                        }
                        ArrayList<AttentionBean> list = new ArrayList<>();
                        for (AttentionEntity entity:data) {
                            AttentionBean bean = new AttentionBean();
                            BBSUserBean user = new BBSUserBean();
                            user.setUserPin(entity.getPin());
                            user.setUserName(entity.getUserName());
                            user.setUserDuty(entity.getDuty());
                            if (!TextUtils.isEmpty(entity.getUserDeptName())){
                                user.setUserAddress(entity.getUserDeptName());
                            }else {
                                user.setUserAddress(entity.getDuty());
                            }

                            user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl()));
                            bean.setUser(user);
                            int type = entity.getType();
                            if (type==1){
                                bean.setAttentionState(0); //设置未关注
                            }else if (type==2){
                                bean.setAttentionState(1); //设置互相关注
                            }else if (type ==3 ){
                                bean.setAttentionState(2); //设置互相关注
                            }
                            list.add(bean);
                        }
                        dataList.addAll(list);
                        boolean pageAvailable = response.getNextPageAvailable();
                        callBack.onResult(pageAvailable);
                    }
                }));
    }

    public interface LogicCallBack{
        void onResult(boolean hasNextPage);
    }

}
